/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.games.gpicalculator.jms;

import it.eng.games.esmi.pti.converter.JAXBConverter;
import it.eng.games.esmi.pti.events.GpiCalculation;
import it.eng.games.esmi.pti.events.TypeAction;
import java.io.Serializable;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnectionFactory;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author plebani
 */
public class JMSConnection {

    //@Resource(mappedName = "jms/DataUpdateTopic")
    private Topic dataUpdateTopic;
    //@Resource(mappedName = "jms/ESMIConnectionFactory")
    private TopicConnectionFactory eSMIConnectionFactory;

    public JMSConnection() {
        try {
            InitialContext jndi = new InitialContext();
            this.eSMIConnectionFactory = (TopicConnectionFactory) jndi.lookup("jms/ESMIConnectionFactory");

            this.dataUpdateTopic = (Topic) jndi.lookup("jms/DataUpdateTopic");
        } catch (NamingException ex) {
            Logger.getLogger(JMSConnection.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }

    public synchronized void sendJMSMessage(String idGpiKpi, String idComponent) throws JMSException {


        GpiCalculation event = new GpiCalculation();



        event.setTime(new Date());
        event.setTypeAction(TypeAction.DataUpdate);
        event.setDbName("EPKB");
        event.setTable("stored_values");
        event.setDataUpdated(idGpiKpi + "," + idComponent);




        Connection connection = null;
        Session session = null;
        try {

            if (eSMIConnectionFactory == null) {

                Logger.getLogger(JMSConnection.class.getName()).log(Level.INFO, "No ConnectionFactory");
            } else {
                connection = eSMIConnectionFactory.createConnection();

                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                MessageProducer messageProducer = null;
                messageProducer = session.createProducer(dataUpdateTopic);

                ObjectMessage tm = session.createObjectMessage();
                tm.setObject(event);
                messageProducer.setTimeToLive(1800000); //30 mins
                messageProducer.send(tm);
            }
        } catch (Exception e) {
            Logger.getLogger(JMSConnection.class.getName()).log(Level.INFO, e.getMessage(), e);

        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", e);
                }
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
}
